//
//  DetailsViewController.m
//  测试小型VIEW的加载
//
//  Created by yishangpin2 on 15/3/5.
//  Copyright (c) 2015年 yishangpin2. All rights reserved.
//

#import "DetailsViewController.h"
#import "Triangle.h"
#import "AppDelegate.h"
#import "DescriptionView.h"
#import "RecentView.h"
#import "ProductView.h"
#import "StoreView.h"
#import "ShippingView.h"
#import "BuyView.h"
#import "GuaranteeView.h"
#import "ReturnPolicyViewController.h"

@interface DetailsViewController (){
    AppDelegate *delegate;
    float view1Height;
    float view2Height;
    float view3Height;
    float view4Height;
    float view5Height;
    float view6Height;
    BuyView *buyview;
    NSDictionary *dic;
}


@end

@implementation DetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",_photoTag);
    
    delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
//    _Photo.image = [UIImage imageNamed:[NSString stringWithFormat:@"fushi%d.jpg",[_photoTag integerValue ]-1000]];
    
    //注册点击buy执行事件的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickBuyButton) name:@"clickBuyButton" object:nil];
    //注册移除buyview的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeBuyView) name:@"removeBuyView" object:nil];
    
    //跳转到退货的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoReturnPolicyDetails:) name:@"ReturnPolicy" object:nil];
    
    //Description的readMore的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoReturnPolicyDetails:) name:@"clickReadMore" object:nil];
    
    //注册收藏按钮改变图标通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeToSave) name:@"changeButton" object:nil];

    Triangle *triangle = [[Triangle alloc]initWithFrame:CGRectMake(0, 0, _ButtonBuy.frame.size.height, _ButtonBuy.frame.size.height)];
    
    triangle.backgroundColor = [UIColor clearColor];
    [self.ScrollView addSubview:triangle];
    
    //设置scrollview
    [self setUpScrollView];
    
    //设置收藏按钮
    [self editOrSaveButton];


//    //添加详情表格
    [self addDescriptionView];
    [self addRecentView];
    [self addProductView];
    [self addStoreView];
    [self addShippingView];
    [self addGuaranteeView];
    
    //设置scrollview滑动长度
    self.ScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.FirstView.frame.size.height-20 + view1Height+view2Height+view3Height+view4Height+view5Height+view6Height);
    
    // Do any additional setup after loading the view.
}


- (void)viewWillAppear:(BOOL)animated{

    self.navigationItem.title = @"Details";
    
}
- (void)viewDidDisappear:(BOOL)animated{
//    delegate.detailsDic = nil;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 判断是否收藏了
- (BOOL)isCollect{
    BOOL isCollect = NO;
    for(NSString *tag in delegate.CollectArray) {
        if ([tag isEqualToString:_photoTag]) {
            isCollect =YES;
        }
    }
    return isCollect;
}

#pragma mark - 设置收藏图标
- (void)editOrSaveButton{
    if ([self isCollect] == NO) {
        [self changeToSave];
    }
}

- (void)changeToSave{
    [_saveButton setImage:[UIImage imageNamed:@"xiaoaixin.png"] forState:0];
    [_saveButton setTitle:@"  Save" forState:0];
    
}

#pragma mark - 添加表格

- (void)addDescriptionView{
    DescriptionView *view1 = [[DescriptionView alloc] init];
    delegate.isHaveHeight = YES;
    //获得添加完内容的view的高度
    view1Height = [view1 addView1];
    //根据返回的高度设置view的高度
    view1.frame = CGRectMake(0, self.FirstView.frame.size.height-50, self.view.frame.size.width, view1Height);
    view1.backgroundColor = [UIColor clearColor];
    //把view添加进scrollview
    [self.ScrollView addSubview:view1];
}

- (void)addRecentView{
    RecentView *view2 = [[RecentView alloc] init];
    delegate.isHaveHeight = YES;
    view2Height = [view2 addView2];
    view2.frame = CGRectMake(0, self.FirstView.frame.size.height+view1Height-50, delegate.width, view2Height);
    NSLog(@"起始位置：：%lf",view1Height);
    view2.backgroundColor = [UIColor clearColor];
    [self.ScrollView addSubview:view2];
}

- (void)addProductView{
    ProductView *view3 = [[ProductView alloc] init];
    delegate.isHaveHeight = YES;
    view3Height = [view3 addView3];
    view3.frame = CGRectMake(0, self.FirstView.frame.size.height+view1Height-50+view2Height, delegate.width, view3Height);
    view3.backgroundColor = [UIColor clearColor];
    [self.ScrollView addSubview:view3];
}

- (void)addStoreView{
    StoreView *view4 = [[StoreView alloc] init];
    delegate.isHaveHeight = YES;
    view4Height = [view4 addView4];
    view4.frame = CGRectMake(0, self.FirstView.frame.size.height+view1Height-50+view2Height+view3Height, delegate.width, view3Height);
    view4.backgroundColor = [UIColor clearColor];
    [self.ScrollView addSubview:view4];
}

- (void)addShippingView{
    ShippingView *view5 = [[ShippingView alloc] init];
    delegate.isHaveHeight = YES;
    view5Height = [view5 addView5];
    view5.frame = CGRectMake(0, self.FirstView.frame.size.height+view1Height-50+view2Height+view3Height+view4Height, delegate.width, view5Height);
    view5.backgroundColor = [UIColor clearColor];
    [self.ScrollView addSubview:view5];
}

- (void)addGuaranteeView{
    GuaranteeView *GView = [[GuaranteeView alloc] initWithFrame:CGRectMake(0, self.FirstView.frame.size.height+view1Height-50+view2Height+view3Height+view4Height+view5Height, delegate.width, delegate.height/9)];
    view6Height = delegate.height/9;
    GView.backgroundColor = [UIColor clearColor];
    [self.ScrollView addSubview:GView];
}

#pragma mark - 点击buy的时间

- (void)clickBuyButton{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"BuyView" owner:self options:nil];
    buyview = nib[0];
    buyview.frame = CGRectMake(0, 0, delegate.width, delegate.height);
    [self.view addSubview:buyview];
}

- (void)removeBuyView{
    [buyview removeFromSuperview];
}

#pragma mark - scrollview设置

- (void)setUpScrollView{
    
    NSMutableArray *picmutArray = [NSMutableArray arrayWithObjects:@"fushi1.jpg",@"fushi2.jpg",@"fushi3.jpg",@"fushi4.jpg",@"fushi5.jpg", nil];
    [picmutArray setObject:[NSString stringWithFormat:@"fushi%d.jpg",[_photoTag intValue]-1000] atIndexedSubscript:0];
    float width = self.imageScrollView.frame.size.width;
    //自动布局引起的（疑似先布局，所以有高度，最后选项卡加入后再布局）
    float height = self.imageScrollView.frame.size.height-50;
    
    for (int index = 0; index < picmutArray.count; index++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:picmutArray[index]]];
        imageView.frame = CGRectMake(20, 20, width-40, height-40);
        imageView.contentMode = UIViewContentModeScaleToFill;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(width*index, 0, width, height)];
        //图片添加进view
        [view addSubview:imageView];
        //view添加进scrollview
        [self.imageScrollView addSubview:view];
    }
    //设置滚动范围
    self.imageScrollView.contentSize = CGSizeMake(picmutArray.count*width, height);
    //隐藏进度条
    self.imageScrollView.showsHorizontalScrollIndicator = NO;
    //scrollview分页
    self.imageScrollView.pagingEnabled = YES;
    //    self.imageScorllView.backgroundColor = [UIColor blueColor];
    NSLog(@"%f,%f",self.imageScrollView.frame.size.height,self.imageScrollView.frame.size.width);
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)EditList:(id)sender {
    
    
    if ([_saveButton.titleLabel.text isEqualToString:@"  Save"]) {
        [delegate.CollectArray addObject:_photoTag];
        [_saveButton setTitle:@"  Edit List" forState:0];
        [_saveButton setImage:[UIImage imageNamed:@"打钩.png"] forState:0];
    }
    else {
        delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
        YRSideViewController *sideViewController=[delegate sideViewController];
        sideViewController.rightViewController.CreateWishlist = @"Create Wishlist";
        [sideViewController showRightViewController:true];
        //显示removeButton
        [[NSNotificationCenter defaultCenter] postNotificationName:@"showRemoveView" object:nil];
        delegate.collectTag = [_photoTag intValue];
    }
}

- (IBAction)clickbuyButton:(id)sender {
    [self clickBuyButton];
}


#pragma mark - readmore和returnpolicy的跳转
- (void)gotoReturnPolicyDetails: (NSNotification *)sender{
    ReturnPolicyViewController *returnPolicy = [self.storyboard instantiateViewControllerWithIdentifier:@"ReturnPolicy"];
    if ([sender.object isEqualToString:@"ReturnPolicyDetails"]) {
        delegate.whichView = @"ReturnPolicyDetails";
    }
    if ([sender.object isEqualToString:@"ReadMore"]) {
        delegate.whichView = @"ReadMore";
    }
    [self presentViewController:returnPolicy animated:YES completion:nil];
    
}

//#pragma mark - 请求数据
//- (void)londing{
//    NSURL *url = [NSURL URLWithString:[delegate.path_URL stringByAppendingString:@"ProductInfo/showproduct"]];
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//    [request setHTTPMethod:@"POST"];
//    NSString *str = @"productId=2";
//    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//    [request setHTTPBody:data];
//    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//        delegate.detailsDic = arr[0];
//        NSLog(@"%@",delegate.detailsDic);
//        [self performSelectorOnMainThread:@selector(refreshData:) withObject:arr waitUntilDone:YES];
//        
//    }];
//}

//- (void)refreshData:(NSArray *)sender{
////    //添加详情表格
////    [self addDescriptionView];
////    [self addRecentView];
////    [self addProductView];
////    [self addStoreView];
////    [self addShippingView];
////    [self addGuaranteeView];
////    
////    //设置scrollview滑动长度
////    self.ScrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.FirstView.frame.size.height-20 + view1Height+view2Height+view3Height+view4Height+view5Height+view6Height);
//}



@end
